package bgate.hung.workerclass;

import java.util.ArrayList;
import java.util.List;

import bgate.hung.activity.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuAdapter extends ArrayAdapter<MenuItem> {
	private Context context;
	private List<MenuItem> menu = new ArrayList<MenuItem>();
	private ImageView imageView;
	private TextView textView;
	
	public MenuAdapter(Context context, int textViewResourceId,
			List<MenuItem> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		menu = objects;
	}
	
	public int getCount() {
		return menu.size();
	}
	
	public MenuItem getItem(int index) {
		return menu.get(index);
	} 

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.menu_listview_item, parent, false);
		}
		MenuItem item = getItem(position);
		imageView = (ImageView) row.findViewById(R.id.imageView_menu_item);
		textView = (TextView) row.findViewById(R.id.textView_menu_item);
		
		imageView.setImageBitmap(item.getItemImage());
		textView.setText(item.getItemName());
		
		if(position == 0) {
			row.setBackgroundResource(R.drawable.menu_back1);
		} else if (position == getCount() - 1) {
			row.setBackgroundResource(R.drawable.menu_back3);
		} else {
			row.setBackgroundResource(R.drawable.menu_back2);
		}
		return row;
	}
}
